package cn.tedu.ivoswork.dictoption.service.impl;

import cn.tedu.ivoswork.base.exception.ServiceException;
import cn.tedu.ivoswork.base.response.StatusCode;
import cn.tedu.ivoswork.dict.mapper.DictMapper;
import cn.tedu.ivoswork.dict.pojo.dto.DictQuery;
import cn.tedu.ivoswork.dict.pojo.entity.Dict;
import cn.tedu.ivoswork.dict.pojo.vo.DictVO;
import cn.tedu.ivoswork.dictoption.mapper.DictOptionMapper;
import cn.tedu.ivoswork.dictoption.pojo.dto.DictOptionQuery;
import cn.tedu.ivoswork.dictoption.pojo.vo.DictOptionVO;
import cn.tedu.ivoswork.dictoption.service.IDictOptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class DictOptionService implements IDictOptionService {
    @Autowired
    DictOptionMapper dictOptionMapper;

    @Autowired
    DictMapper dictMapper;

    @Override
    public List<DictOptionVO> select(DictOptionQuery dictOptionQuery) {
        log.debug("加载字典项业务：参数={}",dictOptionQuery);
        return dictOptionMapper.select(dictOptionQuery);
    }

    @Override
    public List<DictOptionVO> selectDictOptionByCode(String code) {
        log.debug("根据code加载字典项业务：参数={}",code);
        DictQuery dictQuery = new DictQuery();
        dictQuery.setCode(code);
        List<DictVO> list = dictMapper.select(dictQuery);
        if (list != null && ! list.isEmpty()) {
            DictOptionQuery dictOptionQuery = new DictOptionQuery();
            dictOptionQuery.setDictId(list.get(0).getId());
            List<DictOptionVO> dictOptionVOList = dictOptionMapper.select(dictOptionQuery);
            log.debug("result:"+dictOptionVOList.toString());
            return dictOptionVOList;
        }else{
            throw new ServiceException(StatusCode.DATA_UNEXISTS);
        }
    }
}
